﻿using System;
using System.Linq;
using VSIXModel.Entitys;
using VSIXModel.Page;
namespace $model.Namespace
{
    public class ${model.Name}Manage : BaseManage,I${model.Name}Manage
    {
        public PageResult<${model.OutPutDto.Namespace}.${model.OutPutDto.ClassName}> Get${model.Name}Page(BasePage input)
        {
        	var result = new PageResult<${model.OutPutDto.Namespace}.${model.OutPutDto.ClassName}>();
        	using(var db = new VSIXDbContext())
        	{
        		var data = db.${model.name}s.OrderBy(c=>c.${model.TemplateMdoel.Key}).Skip(input.PageSize * (input.PageIndex - 1)).Take(input.PageSize).ToList().ToMaps<${model.OutPutDto.Namespace}.${model.OutPutDto.ClassName}>();
        		result.Items = data;
        		result.Total = db.${model.name}s.Count();
        	}
            return result;
        }

        public void Add${model.Name} (${model.CreateInputDto.Namespace}.${model.CreateInputDto.ClassName} input)
        {
            var entity = input.ToMap<${model.FullName}>();
            using (var db = new VSIXDbContext())
            {
                db.${model.Name}s.Add(entity);
                db.SaveChanges();
            }
        }

        public void Update${model.Name} (${model.UpdateInputDto.Namespace}.${model.UpdateInputDto.ClassName} input)
        {
            using (var db = new VSIXDbContext())
            {
                var entity = db.${model.Name}s.Where(c => c.Id == input.${model.TemplateMdoel.Key}).FirstOrDefault();
                if (entity == null)
                    return;
                entity.Mapping(input);
                db.${model.Name}s.Update(entity);
                db.SaveChanges();
            }
        }

        public void Remove${model.Name} (int ${model.TemplateMdoel.Key})
        {
            using (var db = new VSIXDbContext())
            {
                var entity = db.${model.Name}s.Where(c => c.Id == ${model.TemplateMdoel.Key}).FirstOrDefault();
                if (entity == null)
                    return;
                db.${model.Name}s.Remove(entity);
                db.SaveChanges();
            }
        }

        public ${model.OutPutDto.Namespace}.${model.OutPutDto.ClassName} Get${model.Name}Info(int ${model.TemplateMdoel.Key})
        {
        	using(var db = new VSIXDbContext())
        	{
        		var entity = db.${model.Name}s.Where(c=>c.${model.TemplateMdoel.Key} == ${model.TemplateMdoel.Key}).FirstOrDefault();
        		return entity.ToMap<${model.OutPutDto.Namespace}.${model.OutPutDto.ClassName}>();
        	}
        }
    }
}
